﻿import cv2
import os 
import time

now = time.strftime("%Y-%m-%d",time.localtime(time.time()))
shijian = "D:/Users/User/Desktop/chwh/zs"+'/'+now+r'黑屏检测.txt'
#print (tt)
dir = "D:/Users/User/Desktop/chwh/zs/tt"
im_name = []
wildcard = ".jpg"
exts = wildcard.split(" ")
files = os.listdir(dir)
for name in files:
    fullname = os.path.join(dir , name)
    for ext in exts:
        if (name.endswith(ext)):
             im_name.append(name)
             break

f = open(shijian,'a') 
f.truncate(0)

#读取最大id
shijian1 = "D:/Users/User/Desktop/chwh/zs/sql/maxid.txt"
with open(shijian1,"r") as f:
    data=f.readline()
    data1=int(data)

for i in range(0,500):
    # 把图片转换为单通道的灰度图
    path = 'D:/Users/User/Desktop/chwh/zs/tt'
    #path = tt
    img_path = im_name[i]
    ip = img_path[-20:-5]
    tdh = img_path[-5]
    #img = cv2.imread(img_path)
    img = cv2.imread('D:/Users/User/Desktop/chwh/zs/tt'+'/'+img_path)
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 获取灰度图矩阵的行数和列数
    r, c = gray_img.shape[:2]
    piexs_sum = r * c  # 整个弧度图的像素个数为r*c

    # 获取偏暗的像素(表示0~19的灰度值为暗) 此处阈值可以修改
    dark_points = (gray_img < 20)
    target_array = gray_img[dark_points]
    dark_sum = target_array.size
    # 判断灰度值为暗的百分比
    dark_prop = dark_sum / (piexs_sum)
    if dark_prop >= 0.85:
        ii=str(i+data1+1)
        print("black")
        f = open(shijian,'a') 
        f.write(ip)
        f.write("第")
        f.write(tdh)
        f.write("个通道屏幕黑屏个")
        f.write(ii)
        f.write("\n")
        f.close()
    else:
        ii=str(i+data1+1)
        print("正常")
        f = open(shijian,'a') 
        f.write(ip)
        f.write("第")
        f.write(tdh)
        f.write("个通道正常个")
        f.write(ii)
        f.write("\n")
        f.close()